#!/bin/bash

echo "🚀 Building Smart AI Agent System..."
echo "===================================="

WXWIN="/d/vc/wxWidgets-3.2.7"

# 检查wxWidgets安装
if [ ! -d "$WXWIN" ]; then
    echo "❌ wxWidgets目录不存在: $WXWIN"
    exit 1
fi

# 检查必要的源文件
echo "📋 Checking source files..."
REQUIRED_FILES=(
    "src/main.cpp"
    "src/ai_agent.cpp" 
    "src/agents.cpp"
    "src/modern_chat_app.cpp"
)

MISSING_FILES=0
for file in "${REQUIRED_FILES[@]}"; do
    if [ ! -f "$file" ]; then
        echo "❌ $file not found!"
        MISSING_FILES=1
    fi
done

if [ $MISSING_FILES -eq 1 ]; then
    echo "❌ Missing source files, cannot build"
    exit 1
fi

echo "✅ All source files present"

# 设置包含路径
WX_INCLUDE="$WXWIN/include"
WX_SETUP="$WXWIN/lib/gcc_dll/mswu"

# 检查setup.h文件
if [ ! -f "$WX_SETUP/wx/setup.h" ]; then
    echo "🔍 Searching for wx/setup.h..."
    # 在wxWidgets目录中查找setup.h
    FIND_SETUP=$(find "$WXWIN" -name "setup.h" | grep "mswu" | head -1)
    if [ -n "$FIND_SETUP" ]; then
        WX_SETUP=$(dirname "$FIND_SETUP")
        echo "✅ Found setup.h in: $WX_SETUP"
    else
        echo "❌ Cannot find wx/setup.h"
        echo "💡 Please build wxWidgets first or check installation"
        exit 1
    fi
fi

echo "📁 Using include paths:"
echo "   - $WX_INCLUDE"
echo "   - $WX_SETUP"
echo "   - src"

# 构建命令
echo "🔨 Building AI Agent System..."
g++ -std=c++17 -O2 \
    -I"$WX_INCLUDE" \
    -I"$WX_SETUP" \
    -I"src" \
    src/main.cpp \
    src/ai_agent.cpp \
    src/agents.cpp \
    src/modern_chat_app.cpp \
    -L"$WXWIN/lib/gcc_dll" \
    -lwx_mswu_xrc-3.2 \
    -lwx_mswu_html-3.2 \
    -lwx_mswu_qa-3.2 \
    -lwx_mswu_adv-3.2 \
    -lwx_mswu_core-3.2 \
    -lwx_baseu_xml-3.2 \
    -lwx_baseu_net-3.2 \
    -lwx_baseu-3.2 \
    -lwxscintilla-3.2 \
    -mwindows -lopengl32 -lglu32 \
    -o modern_ai_chat.exe

if [ $? -eq 0 ]; then
    echo ""
    echo "🎉 ✅ Build successful!"
    echo "📦 Output: modern_ai_chat.exe"
    echo ""
    echo "🚀 运行方式:"
    echo "   ./modern_ai_chat.exe"
    echo "   ./modern_ai_chat.exe --MCPServer --AGI Coder"
    echo ""
    echo "💡 功能特色:"
    echo "   • 💬 智能对话助手 - 类似DeepSeek"
    echo "   • 🔧 代码生成器 - 多种编程语言" 
    echo "   • 🔍 代码分析器 - 项目结构分析"
else
    echo "❌ Build failed"
    exit 1
fi
